package com.hhang.boot.controller;

import com.github.pagehelper.PageInfo;
import com.hhang.boot.exception.ServiceException;
import com.hhang.boot.service.HouseService;
import com.hhang.boot.utils.ResponseDataBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController

@RequestMapping("/house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    //配置文件里的路径
    @Value("${file.upload.path}")
    private String path;
    //茶室 部分
    @GetMapping("/findHouse")
    public Map<String,Object> findHouse(@RequestParam Map<String,Object> param){
        try {
            PageInfo<Map<String,Object>> byName = houseService.findHouse(param);
            return new ResponseDataBuilder().success().data(byName).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }
    @PostMapping("/addHouse")
    public Map<String,Object> addHouse(@RequestParam("file") MultipartFile file, @RequestParam Map<String,Object> param) throws IOException {
        //上传的图片规定一个唯一一个随机名称放入库中
        String uuid = UUID.randomUUID().toString();
        String filename = file.getOriginalFilename();
        assert filename != null;
        String externalName = filename.substring(filename.lastIndexOf("."));
        String storeName = uuid + externalName;
        file.transferTo(new File(path +storeName));
        param.put("file",storeName);
        try {
            int res= houseService.addHouse(param);
            return new ResponseDataBuilder().success().message("添加成功").data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }
    @DeleteMapping("/delHouse")
    public Map<String,Object> delHouse(@RequestParam (value = "ids") Long[] ids){
        try {
            int res = houseService.delHouse(Arrays.asList(ids));
            return new ResponseDataBuilder().success().data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }
    @PostMapping("/upHouse")
    public Map<String,Object> upHouse(@RequestBody Map<String,Object> param){
        try {
            int res = houseService.upHouse(param);
            return new ResponseDataBuilder().data(res).success().build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c更新失败").build();
        }
    }

    // 资金部分
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody Map<String,Object> param){
        try {
            int res = houseService.add(param);
            return new ResponseDataBuilder().success().message("添加成功").data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c服务异常").build();
        }
    }

    @DeleteMapping("/del")
    public Map<String,Object> del(@RequestParam (value = "id") Long[] id){
        try {
            int res = houseService.del(Arrays.asList(id));
            return new ResponseDataBuilder().success().data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }

    @PostMapping("/upDate")
    public Map<String,Object> upDate(@RequestBody Map<String,Object> param){
        try {
            int res = houseService.update(param);
            return new ResponseDataBuilder().data(res).success().build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c更新失败").build();
        }
    }
    @GetMapping("/findPrice")
    public Map<String,Object> findPrice(@RequestParam Map<String,Object> note){
        try {
            PageInfo<Map<String,Object>> list = houseService.findPrice(note);
            return new ResponseDataBuilder().success().data(list).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }
    @GetMapping("/find7days")
    public Map<String,Object> find7days(@RequestParam Map<String,Object> note){
        try {
            List<Map<String,Object>> list = houseService.find7days(note);
            return new ResponseDataBuilder().success().data(list).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }



    //分店部分 /house/findBranch
    @GetMapping("/findBranch")
    public Map<String,Object> findBranch(@RequestParam Map<String,Object> note){
        try {
            PageInfo<Map<String,Object>> list = houseService.findBranch(note);
            return new ResponseDataBuilder().success().data(list).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }
    @DeleteMapping("/delBranch")
    public Map<String,Object> delBranch(@RequestParam (value = "ids") Long[] id){
        try {
            int res = houseService.delBranch(Arrays.asList(id));
            return new ResponseDataBuilder().success().data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }
    @PostMapping("/addBranch")
    public Map<String,Object> addBranch(@RequestBody Map<String,Object> param){
        try {
            int res = houseService.addBranch(param);
            return new ResponseDataBuilder().success().message("添加成功").data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c服务异常").build();
        }
    }
}
